package com.example.shuiyin.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("users")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    
    @TableField("username")
    private String username;
    
    @TableField("email")
    private String email;
    
    @TableField("phone")
    private String phone;
    
    @TableField("password")
    private String password;
    
    @TableField("salt")
    private String salt;
    
    @TableField("avatar")
    private String avatar;
    
    // 非数据库字段，用于前端显示
    @TableField(exist = false)
    private String avatarUrl;
    
    @TableField("nickname")
    private String nickname;
    
    @TableField("role")
    private String role;
    
    @TableField("user_level")
    private Integer userLevel;
    
    @TableField("openid")
    private String openid;
    
    @TableField("reset_token")
    private String resetToken;
    
    @TableField("reset_token_expire_time")
    private LocalDateTime resetTokenExpireTime;
    
    @TableField("created_at")
    private LocalDateTime createdAt;
    
    @TableField("updated_at")
    private LocalDateTime updatedAt;
    
    @TableField("status")
    private String status;
    
    /**
     * 获取头像URL，处理相对路径和绝对路径
     */
    public String getAvatarUrl() {
        if (avatar == null || avatar.isEmpty()) {
            return null;
        }
        
        // 如果avatar已经是完整URL则直接返回
        if (avatar.startsWith("http://") || avatar.startsWith("https://")) {
            return avatar;
        }
        
        // 否则拼接URL前缀
        return "/api/avatar/" + avatar;
    }
    
    /**
     * 设置头像URL
     */
    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }
    
    /**
     * 获取用户ID
     */
    public Long getId() {
        return id;
    }
    
    /**
     * 获取用户名
     */
    public String getUsername() {
        return username;
    }
    
    /**
     * 获取邮箱
     */
    public String getEmail() {
        return email;
    }
    
    /**
     * 获取手机号
     */
    public String getPhone() {
        return phone;
    }
    
    /**
     * 获取头像
     */
    public String getAvatar() {
        return avatar;
    }
    
    /**
     * 获取昵称
     */
    public String getNickname() {
        return nickname;
    }
    
    /**
     * 获取角色
     */
    public String getRole() {
        return role;
    }
    
    /**
     * 获取用户等级
     */
    public Integer getUserLevel() {
        return userLevel;
    }
    
    /**
     * 获取创建时间
     */
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    /**
     * 获取重置令牌过期时间
     */
    public LocalDateTime getResetTokenExpireTime() {
        return resetTokenExpireTime;
    }
    
    /**
     * 获取用户状态
     */
    public String getStatus() {
        return status;
    }
    
    /**
     * 设置用户状态
     */
    public void setStatus(String status) {
        this.status = status;
    }
}